var editId = ''
var pageNum = 1;
var route = '';
var pageNum = 1;
var total = 0;
// 显示新增地址弹窗
$("#add_address").on("click",function(){
    $(".add_address,#zz").show();
})
//X 号隐藏新增弹窗
$(".l_close").on("click",function(){
    $(".add_address,#zz").hide();
    $('.l_address').val('');
    $('.l_name').val('');
    $('.l_phone').val('');
});
// 取消隐藏新增弹窗
$(".a_cancel").on("click",function(){
    $(".add_address,#zz").hide();
    $('.l_address').val('');
    $('.l_name').val('');
    $('.l_phone').val('');
});
// 显示编辑地址弹窗
$("#edit_address").on("click",function(){
    $(".edit_address,#zz").show();
})
//X 号隐藏编辑弹窗
$(".edit_close").on("click",function(){
    $(".edit_address,#zz").hide();
    $('.x_address').val('');
    $('.x_name').val('');
    $('.x_phone').val('');
});
// 取消隐藏编辑弹窗
$(".a_cancel").on("click",function(){
    $(".edit_address,#zz").hide();
    $('.x_address').val('');
    $('.x_name').val('');
    $('.x_phone').val('');
});
// 编辑事件
function editAddress (value) {
    $.ajax({
        type:"get",
        url: upUrl + "/receivingAddress/getDetailByRid",
        dataType: "json",
        data: {
            id: value,
        },
        success:function(data){
            $("input[name='x_address']").val(data.result.address);
            $("input[name='x_name']").val(data.result.receiver);
            $("input[name='x_phone']").val(data.result.telephone);
            $(".edit_address,#zz").show();
            editId = value;
        }
    })
}
var pageNum = 1;
var route = '';
var pageNum = 1;
var total = 0;
$(function(){
    getAddressList();
});
// 获取地址列表数据
function getAddressList () {
    $.ajax({
        type:"get",
        url: upUrl + "/receivingAddress/front/receivingAddressList",
        dataType: "json",
        data: {
            pageNum: pageNum,
            pageSize: 8,
        },
        success:function(data){
            var testList = data.result.records;
            total = data.result.total;
            var orderBody = '';
            $.each(testList, function (index, val) {
                orderBody += '<div class="order_list">\
                        <div>' + val.address + '</div>\
                        <div>' + val.receiver + '</div>\
                        <div>' + val.telephone + '</div>\
                        <div>\
                             <span onclick="editAddress(' + val.id + ')" class="editClass">修改</span>\
                            <span onclick="deleteAddress(' + val.id + ')" class="deleteClass">删除</span>\
                        </div>\
                    </div>'
            })
            $('.item_box').html(orderBody);
            new Pagination({
                element: '#pages_1',
                type: 2,
                pageIndex: pageNum,
                pageSize: 8,
                pageCount: 9,
                total: total,
                jumper: true,
                singlePageHide: false,
                disabled: true,
                currentChange: function(index) {
                    console.log(index);
                    pageNum = index;
                    getAddressList();
                }
            });
        }
    })
}
// 删除地址事件
function deleteAddress (value) {
    $.ajax({
        type:"post",
        url: upUrl + "/receivingAddress/deleteReceivingAddress",
        data: {
            id: value,
        },
        success:function(data){
            if (data.code == 200) {
                toastr.success("删除成功!");
                getAddressList();
            } else {
                toastr.error(data.message);
            }
        }
    })
}
//新增取消事件
function addCancel () {
    $(".add_address,#zz").hide();
    $('.l_address').val('');
    $('.l_name').val('');
    $('.l_phone').val('');
}
// 新增确认接口
function addAddressOk (value) {
    if ($('.l_address').val() =='') {
        toastr.warning("请输入地址!");
        return;
    } else if ($('.l_name').val() =='') {
        toastr.warning("请输入联系人!");
        return;
    } else if ($('.l_phone').val() =='') {
        toastr.warning("请输入联系电话!");
        return;
    } else {
        $.ajax({
            type:"post",
            url: upUrl + "/receivingAddress/addReceivingAddress",
            data: {
                address: $('.l_address').val(),
                receiver: $('.l_name').val(),
                telephone: $('.l_phone').val(),
            },
            success:function(data){
                if (data.code == 200) {
                    toastr.success("新增成功!");
                    $(".add_address,#zz").hide();
                    $('.l_address').val('');
                    $('.l_name').val('');
                    $('.l_phone').val('');
                    getAddressList();
                } else {
                    toastr.error(data.message);
                }
            }
        })
    }
}
// 编辑确认接口
function editConfirmClick (value) {
    if ($('.x_address').val() =='') {
        toastr.warning("请输入地址!");
        return;
    } else if ($('.x_name').val() =='') {
        toastr.warning("请输入联系人!");
        return;
    } else if ($('.x_phone').val() =='') {
        toastr.warning("请输入联系电话!");
        return;
    } else {
        $.ajax({
            type:"post",
            url: upUrl + "/receivingAddress/updateReceivingAddress",
            data: {
                id: editId,
                address: $('.x_address').val(),
                receiver: $('.x_name').val(),
                telephone: $('.x_phone').val(),
            },
            success:function(data){
                if (data.code == 200) {
                    toastr.success("新增成功!");
                    $(".edit_address,#zz").hide();
                    $('.x_address').val('');
                    $('.x_name').val('');
                    $('.x_phone').val('');
                    getAddressList();
                } else {
                    toastr.error(data.message);
                }
            }
        })
    }
}